//
//  ThreeImageTableViewCell.m
//  Wangi
//
//  Created by ChuXiang on 15/11/25.
//  Copyright © 2015年 ChuXiang. All rights reserved.
//

#import "ThreeImageTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+UIViewAdditions.h"

#define kWidth (self.width - 6) / 3
@interface ThreeImageTableViewCell ()
@property (nonatomic, retain) UIImageView *image1;
@property (nonatomic, retain) UIImageView *image2;
@property (nonatomic, retain) UIImageView *image3;
@property (nonatomic, retain) UILabel *titleLabel;
@end
@implementation ThreeImageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.image1];
        [self addSubview:self.image2];
        [self addSubview:self.image3];
        [_titleLabel release];
        [_image1 release];
        [_image2 release];
        [_image3 release];
    }
    return self;
}

- (void)setModel:(BaseModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    if (_model.imgextra.count == 0) {
        [_image1 sd_setImageWithURL:[NSURL URLWithString:_model.imgsrc]];
        [_image2 sd_setImageWithURL:[NSURL URLWithString:_model.imgnewextra.firstObject[@"imgsrc"]]];
        [_image3 sd_setImageWithURL:[NSURL URLWithString:_model.imgnewextra.lastObject[@"imgsrc"]]];
    }else {
    [_image1 sd_setImageWithURL:[NSURL URLWithString:_model.imgsrc]];
    [_image2 sd_setImageWithURL:[NSURL URLWithString:_model.imgextra.firstObject[@"imgsrc"]]];
    [_image3 sd_setImageWithURL:[NSURL URLWithString:_model.imgextra.lastObject[@"imgsrc"]]];
    }
    _titleLabel.text = _model.title;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(5, 5, self.width - 30, 20);
    self.image1.frame = CGRectMake( 5, _titleLabel.bottom + 2, kWidth - 5, self.height - _titleLabel.height - 10);
    self.image2.frame = CGRectMake(5 + kWidth,  _image1.top, kWidth - 5, _image1.height);
    self.image3.frame = CGRectMake(5 + kWidth * 2, _image1.top, kWidth - 5, _image1.height);
}

- (void)setData:(NSArray *)data {
    if (_data != data) {
        [_data release];
        _data = [data retain];
    }
}

#pragma mark - 懒加载
- (UIImageView *)image1 {
    if (!_image1) {
        self.image1 = [[UIImageView alloc] initWithFrame:CGRectZero];
        [_image1 release];
    }
    return _image1;
}

- (UIImageView *)image2 {
    if (!_image2) {
        self.image2 = [[UIImageView alloc] initWithFrame:CGRectZero];
        [_image2 release];
    }
    return _image2;
}

- (UIImageView *)image3 {
    if (!_image3) {
        self.image3 = [[UIImageView alloc] initWithFrame:CGRectZero];
        [_image3 release];
    }
    return _image3;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [_titleLabel release];
    }
    return _titleLabel;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
